import AppStore from "../store/appStore";
import axiosUtil from "../utils/axiosUtil";
import viewUtil from '../utils/viewUtil';
import {CreateHaoDanwangList} from "./modeCtr";

export function getTaoBaoConfig() {
  return new Promise(resolve => {
    Taro.request({
      method:'GET',
      url:"https://xiaotaoliangmen.oss-cn-beijing.aliyuncs.com/tbconfigs.json",
    }).then(res=>{
      // console.log(res)
      const mydata = res.data
      AppStore.appConfig.open=mydata.Open
      resolve(mydata.Open);
    })
  })

}

export function getTaoBaoData() {
  return new Promise(resolve => {
    Taro.request({
      method:'GET',
      url:"https://xiaotaoliangmen.oss-cn-beijing.aliyuncs.com/taobaoData.json",
    }).then(res=>{
      // console.log(res)
      const mydata = res.data
      let ttt = CreateHaoDanwangList(mydata.data);
      resolve({data: ttt, Notice: mydata.Notice,auth:mydata.auth});
    })
})


}

export function getHomeItemData(item) {
  return new  Promise(resolve => {
    var url = ''
    // console.log(item.api)
    switch (item.api) {
      case 1://差异化货品API
        url = AppStore.data.taoBaoApi.tb_items_material_data.format(AppStore.data.taoBaoApi.haoDanKey,item.cat_id,item.curPage)
        // console.log(url)
        axiosUtil.get( url).then(res=>{
          // console.log(res)
          if (parseInt(res.code) !== 200){
            viewUtil.ShowToast('服务器错误～');
          }else{
            if(res.data.length <=0)
            {
              viewUtil.ShowToast(res.msg);
              return
            }
            let ttt = CreateHaoDanwangList(res.data);
            resolve({data: ttt, pageIndex: res.min_id});
          }
        })
        break;
      case 2:  //今日推荐
        url = AppStore.data.taoBaoApi.deserve_item.format(AppStore.data.taoBaoApi.haoDanKey)
        axiosUtil.get( url).then(res=>{
          // console.log(res)
          if (parseInt(res.code) !== 1){
            viewUtil.ShowToast('服务器错误～');
          }else{
            if(res.item_info.length <=0)
            {
              viewUtil.ShowToast(res.msg);
              return
            }
            let ttt = CreateHaoDanwangList(res.item_info);
            resolve({data: ttt, pageIndex: res.min_id});
          }
        })
        break;
      case 3:
        url = AppStore.data.taoBaoApi.ienteeringitems.format(AppStore.data.taoBaoApi.haoDanKey,item.curPage)
        axiosUtil.get( url).then(res=>{
          // console.log(res)
          if (parseInt(res.code) !== 200){
            viewUtil.ShowToast('服务器错误～');
          }else{
            if(res.data.length <=0)
            {
              viewUtil.ShowToast(res.msg);
              return
            }
            let ttt = CreateHaoDanwangList(res.data);
            resolve({data: ttt, pageIndex: res.min_id});
          }
        })
        break;
      case 4:
        url = AppStore.data.taoBaoApi.low_price_Pinkage_data.format(AppStore.data.taoBaoApi.haoDanKey,item.cat_id,item.curPage)
        axiosUtil.get( url).then(res=>{
          // console.log(res)
          if (parseInt(res.code) !== 1){
            viewUtil.ShowToast('服务器错误～');
          }else{
            if(res.data.length <=0)
            {
              viewUtil.ShowToast(res.msg);
              return
            }
            let ttt = CreateHaoDanwangList(res.data);
            resolve({data: ttt, pageIndex: res.min_id});
          }
        })
        break;
      case 5: //高佣专场商品
        url = AppStore.data.taoBaoApi.highitems.format(AppStore.data.taoBaoApi.haoDanKey,item.curPage)
        axiosUtil.get( url).then(res=>{
          // console.log(res)
          if (parseInt(res.code) !== 200){
            viewUtil.ShowToast('服务器错误～');
          }else{
            if(res.data.length <=0)
            {
              viewUtil.ShowToast(res.msg);
              return
            }
            let ttt = CreateHaoDanwangList(res.data);
            resolve({data: ttt, pageIndex: res.min_id});
          }
        })
      case 6: //高于多少优惠卷
        url = AppStore.data.taoBaoApi.coupons.format(item.cat_id,item.curPage)
        // console.log(url)
        axiosUtil.get( url).then(res=>{
          // console.log(res)
          if (parseInt(res.code) !== 200){
            viewUtil.ShowToast('服务器错误～');
          }else{
            if(res.item_info.length <=0)
            {
              viewUtil.ShowToast(res.msg);
              return
            }
            let ttt = CreateHaoDanwangList(res.item_info);
            resolve({data: ttt, pageIndex: item.curPage+1});
          }
        })
        break;

    }
  })
}


